/**
 * Copyright 2018-2019 jianggujin (www.jianggujin.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jianggujin.modulelink.magager;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import com.jianggujin.modulelink.config.JModuleConfig;
import com.jianggujin.modulelink.exception.JModuleNotFoundException;
import com.jianggujin.modulelink.loader.JDefaultModuleLoader;
import com.jianggujin.modulelink.loader.JModuleLoader;
import com.jianggujin.modulelink.module.JModule;
import com.jianggujin.modulelink.module.JModuleListener;
import com.jianggujin.modulelink.support.JBeanProvider;
import com.jianggujin.modulelink.support.JEmptyBeanProvider;
import com.jianggujin.modulelink.support.plugin.JInterceptor;
import com.jianggujin.modulelink.util.JAssert;
import com.jianggujin.modulelink.util.JModuleUtils;

/**
 * 模块管理器抽象实现
 * 
 * @author jianggujin
 *
 */
public abstract class JAbstractModuleManager<T, C extends JModuleConfig> implements JModuleManagerSupport<C> {
    /**
     * 已注册的所有模块,key:moduleName
     */
    protected final Map<String, T> modules = new ConcurrentHashMap<String, T>();

    /**
     * 模块加载器
     */
    private JModuleLoader<C> moduleLoader;
    /**
     * Bean提供者服务
     */
    private JBeanProvider beanProvider;
    /**
     * 模块监听器
     */
    private List<JModuleListener> moduleListeners;
    /**
     * 拦截器
     */
    private List<JInterceptor> interceptors;

    @Override
    public boolean has(String moduleName) {
        return moduleName != null && this.modules.containsKey(moduleName);
    }

    @Override
    public Set<String> getModuleNames() {
        return this.modules.keySet();
    }

    @Override
    public void setModuleLoader(JModuleLoader<C> moduleLoader) {
        this.moduleLoader = moduleLoader;
    }

    @Override
    public JModuleLoader<C> getModuleLoader() {
        if (this.moduleLoader == null) {
            this.moduleLoader = new JDefaultModuleLoader<C>();
        }
        return moduleLoader;
    }

    @Override
    public JModule<C> getModule(String moduleName) {
        JModule<C> module = this.optModule(moduleName);
        if (module == null) {
            throw new JModuleNotFoundException("could not found module with module name:" + moduleName);
        }
        return module;
    }

    @Override
    public void setModuleListeners(List<JModuleListener> moduleListeners) {
        this.moduleListeners = moduleListeners;
    }

    @Override
    public List<JModuleListener> getModuleListeners() {
        return this.moduleListeners;
    }

    @Override
    public void addModuleListener(JModuleListener moduleListener) {
        JAssert.checkNotNull(moduleListener, "moduleListener must not be null");
        if (this.moduleListeners == null) {
            this.moduleListeners = new ArrayList<JModuleListener>();
        }
        this.moduleListeners.add(moduleListener);
    }

    @Override
    public synchronized void setInterceptors(List<JInterceptor> interceptors) {
        this.interceptors = interceptors;
        if (this.interceptors != null) {
            JModuleUtils.sortOrder(this.interceptors);
        }
    }

    @Override
    public List<JInterceptor> getInterceptors() {
        return this.interceptors;
    }

    @Override
    public synchronized void addInterceptor(JInterceptor interceptor) {
        JAssert.checkNotNull(interceptor, "interceptor must not be null");
        if (this.interceptors == null) {
            this.interceptors = new ArrayList<JInterceptor>();
        }
        this.interceptors.add(interceptor);
        JModuleUtils.sortOrder(this.interceptors);
    }

    @Override
    public JBeanProvider getBeanProvider() {
        if (this.beanProvider == null) {
            return JEmptyBeanProvider.getInstance();
        }
        return this.beanProvider;
    }

    @Override
    public void setBeanProvider(JBeanProvider beanProvider) {
        this.beanProvider = beanProvider;
    }
}
